package com.lsf.studentmanagerup.servlet.student;

import com.lsf.studentmanagerup.bean.Student;
import com.lsf.studentmanagerup.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 李树飞
 */
@WebServlet(name = "AddStudentServlet", urlPatterns = "/addStudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //获取参数保存到student对象中
        Student student = new Student();
        student.setId(Integer.parseInt(request.getParameter("id")));
        student.setName(request.getParameter("name"));
        student.setGender(request.getParameter("gender"));
        student.setBirthday(request.getParameter("birthday"));
        student.setEmail(request.getParameter("email"));
        student.setComment(request.getParameter("comment"));
        student.setCid(Integer.parseInt(request.getParameter("cid")));

        //调用服务层新增学生的方法
        StudentService studentService = new StudentService();
        studentService.addStudentService(student);

        //重定向到分页查询
        response.sendRedirect("getUserByPage");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
